package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/label")
//跨域访问
@CrossOrigin
//指定当前类也纳入刷新范围,修改配置文件(自定义内容,定义了该类属性的值)后,读取修改后的配置文件后
//(作用:会将该类对象重新创建,将修改后自定义配置文件内容读取到对象属性中),但是如果类
//中修改代码的话,修改代码不会自动生效,因为只重新创建新的对象,不会重新加载类的字节码文件
@RefreshScope
public class LabelController {
    @Autowired
    private LabelService labelService;
    @Value("${hehe.heihei}")
    private String haha;

    // 增
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Label label) {
        labelService.save(label);
        return new Result(StatusCode.OK, true, "添加成功");
    }

    // 删
    @RequestMapping(value = "/{labelId}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String labelId) {
        labelService.delete(labelId);
        return new Result(StatusCode.OK, true, "删除成功");
    }

    // 改
    @RequestMapping(value = "/{labelId}", method = RequestMethod.PUT)
    public Result update(@PathVariable String labelId, @RequestBody Label label) {

        labelService.update(labelId,label);
        return new Result(StatusCode.OK, true, "修改成功");
    }

    // 查  根据id
    @RequestMapping(value = "/{labelId}", method = RequestMethod.GET)
    public Result findById(@PathVariable String labelId) {
        System.out.println(haha);
        System.out.println("测试负载均衡1");
        Label label = labelService.findById(labelId);
        return new Result(StatusCode.OK, true, "查询成功",label);
    }
    // 查 查询所有
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        List<Label> labelList = labelService.findAll();
        return new Result(StatusCode.OK, true, "查询成功",labelList);
    }
    // 查 条件查询
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findByCondition(@RequestBody Label label) {
        List<Label> labelList = labelService.findByCondition(label);
        return new Result(StatusCode.OK, true, "查询成功",labelList);
    }
    // 查 分页查询
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findPageByCondition(@PathVariable Integer page, @PathVariable Integer size, @RequestBody Label label) {
        PageResult<Label> labelPageResult = labelService.findPageByCondition(page, size, label);
        return new Result(StatusCode.OK, true, "查询成功",labelPageResult);
    }
}
